package service

import (
	"awesomeProject/internal/model"
	"awesomeProject/internal/repository"
	"context"
	"strings"
)

type CategoryService interface {
	CreateCategory(ctx context.Context, req *CreateCategoryRequest) (*model.Category, error)
	GetCategory(ctx context.Context, categoryID uint) (*model.Category, error)
	GetCategoryByName(ctx context.Context, name string) (*model.Category, error)
	UpdateCategory(ctx context.Context, categoryID uint, req *UpdateCategoryRequest) error
	DeleteCategory(ctx context.Context, categoryID uint) error
	GetCategoryList(ctx context.Context) ([]*model.Category, error)
}

type categoryService struct {
	categoryRepo repository.CategoryRepository
}

// NewCategoryService 创建分类服务实例
func NewCategoryService(categoryRepo repository.CategoryRepository) CategoryService {
	return &categoryService{
		categoryRepo: categoryRepo,
	}
}

// 请求结构体定义
type CreateCategoryRequest struct {
	Name        string `json:"name" binding:"required,min=1,max=50"`
	Description string `json:"description" binding:"omitempty,max=200"`
	Sort        int    `json:"sort" binding:"omitempty,min=0"`
}

type UpdateCategoryRequest struct {
	Name        string `json:"name" binding:"omitempty,min=1,max=50"`
	Description string `json:"description" binding:"omitempty,max=200"`
	Sort        int    `json:"sort" binding:"omitempty,min=0"`
}

// CreateCategory 创建分类
func (s *categoryService) CreateCategory(ctx context.Context, req *CreateCategoryRequest) (*model.Category, error) {
	category := &model.Category{
		Name:        strings.TrimSpace(req.Name),
		Description: strings.TrimSpace(req.Description),
		Sort:        req.Sort,
	}

	if err := s.categoryRepo.Create(ctx, category); err != nil {
		return nil, err
	}

	return category, nil
}

// GetCategory 获取分类详情
func (s *categoryService) GetCategory(ctx context.Context, categoryID uint) (*model.Category, error) {
	return s.categoryRepo.GetByID(ctx, categoryID)
}

// GetCategoryByName 根据名称获取分类
func (s *categoryService) GetCategoryByName(ctx context.Context, name string) (*model.Category, error) {
	return s.categoryRepo.GetByName(ctx, name)
}

// UpdateCategory 更新分类
func (s *categoryService) UpdateCategory(ctx context.Context, categoryID uint, req *UpdateCategoryRequest) error {
	category, err := s.categoryRepo.GetByID(ctx, categoryID)
	if err != nil {
		return err
	}

	if req.Name != "" {
		category.Name = strings.TrimSpace(req.Name)
	}
	if req.Description != "" {
		category.Description = strings.TrimSpace(req.Description)
	}
	if req.Sort >= 0 {
		category.Sort = req.Sort
	}

	return s.categoryRepo.Update(ctx, category)
}

// DeleteCategory 删除分类
func (s *categoryService) DeleteCategory(ctx context.Context, categoryID uint) error {
	return s.categoryRepo.Delete(ctx, categoryID)
}

// GetCategoryList 获取分类列表
func (s *categoryService) GetCategoryList(ctx context.Context) ([]*model.Category, error) {
	return s.categoryRepo.List(ctx)
}
